package com.tanhua.mqlistener;

import com.tanhua.domain.PublishScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        topic = "tanhua-bigdata",
        consumerGroup="tanhua-bigdata-c"
)
@Component
public class BigDataListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override  //队列中有数据 该方法就执行
    public void onMessage(Map map) {

        Integer userId =Integer.parseInt(map.get("userId").toString());
        Integer pid =Integer.parseInt(map.get("pid").toString());
        Integer type =Integer.parseInt(map.get("type").toString()); //行为--》分数


        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId.longValue());
        publishScore.setDate(System.currentTimeMillis());
        publishScore.setPublishId(pid.longValue());


        Double score = 0D;
        /*- 操作1： 浏览 +1
        - 操作2： 点赞 +5
        - 操作3： 喜欢 +8
        - 操作4： 评论 + 10
        - 操作5：取消点赞 -5
        - 操作6：取消喜欢 -8*/
        switch (type){
            case 1:{
                score = 1D;
                break;
            }case 2:{
                score = 5D;
                break;
            }case 3:{
                score = 8D;
                break;
            }case 4:{
                score = 10D;
                break;
            }case 5:{
                score = -5D;
                break;
            }case 6:{
                score = -8D;
                break;
            }default:{
                break;
            }
        }

        publishScore.setScore(score);

        //插入
        mongoTemplate.insert(publishScore);

        System.out.println("行为已经放入mongo...");
    }





}
